package com.lw.netty.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class DiscardServerHandler extends ChannelHandlerAdapter {
	public static PooledByteBufAllocator allocator = new PooledByteBufAllocator();

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf buf = (ByteBuf) msg;
		int s = 0;
		while (buf.isReadable()) { // (1)
			s = buf.readInt();
			System.out.println("服务端接受到的数据是：" + s);
		}
		ByteBuf wbuf = allocator.buffer().writeBytes(("11111").getBytes("utf-8"));
		ctx.writeAndFlush(wbuf);
		System.out.println("输出给客户端完毕！");
	}

}
